// <copyright file="OCLPSInstruction.cs" company="Codeplex project Eco.Contrib"> 
// The MIT License (MIT) 
// </copyright> 
// <author>Peter Buchmann</author> 

namespace Eco.Contrib
{
    using System;
    using System.Collections.Generic;
    using Eco.ObjectRepresentation;
    using Eco.Services;
    using Eco.UmlCodeAttributes;

    /// <summary>
    /// OCLPSInstruction: Instruction that represents a OclPs query
    /// </summary>
    public partial class OclpsInstruction
    {
        /// <summary>
        /// Executes the function.
        /// </summary>
        /// <param name="serviceProvider">The service provider.</param>
        /// <param name="variableList">The variable list.</param>
        /// <returns>Return value of executed function</returns>
        [UmlElement(Id = "446db73d-3c0f-4d4e-ae09-177ccdd8fad0")]
        protected internal override IElement ExecuteFunction(IEcoServiceProvider serviceProvider, IModifiableVariableList variableList)
        {
            IOclPsService oclPsService = serviceProvider.GetEcoService<IOclPsService>();
            IElement element = oclPsService.Execute(null, OCL, variableList);
            SetVariable(serviceProvider, variableList, element);
            return element;
        }

        /// <summary>
        /// Inits the specified tokens.
        /// </summary>
        /// <param name="tokens">The tokens.</param>
        [UmlElement(Id = "b9105a52-be8a-451a-9495-5b5d326032c9")]
        protected internal override void Init(Dictionary<IList<EslToken>, Type> tokens)
        {
            List<EslToken> tokenList = new List<EslToken>();
            tokenList.Add(new EslToken(EslTokenType.Word, "OclPs"));
            tokenList.Add(new EslToken(EslTokenType.Operator, "("));
            tokens.Add(tokenList, this.GetType());
            tokenList = new List<EslToken>();
            tokenList.Add(new EslToken(EslTokenType.Word, "%"));
            tokenList.Add(new EslToken(EslTokenType.Operator, ":="));
            tokenList.Add(new EslToken(EslTokenType.Word, "OclPs"));
            tokenList.Add(new EslToken(EslTokenType.Operator, "("));
            tokens.Add(tokenList, this.GetType());
        }

        /// <summary>
        /// Parses the specified token list.
        /// </summary>
        /// <param name="tokenList">The token list.</param>
        /// <param name="ecoScript">The eco script.</param>
        /// <param name="fullExpression">if set to <c>true</c> [full expression].</param>
        [UmlElement(Id = "33a6b62c-c888-40e4-807e-982e7681d937")]
        protected internal override void Parse(IList<EslToken> tokenList, EcoScript ecoScript, bool fullExpression)
        {
            EslToken token = GetNextToken(tokenList);
            RememberEcoScript(ecoScript, token);
            EslToken tokenOperator = GetNextToken(tokenList);

            if ((token.TokenType == EslTokenType.Word) &&
                 (tokenOperator.TokenType == EslTokenType.Operator) && (tokenOperator.TokenValue == ":="))
            {
                ResultVariable = new Variable(this.ServiceProvider());
                ResultVariable.Name = token.TokenValue;
                token = GetNextToken(tokenList);
                tokenOperator = GetNextToken(tokenList);
            }

            EslToken firstToken = null;
            int openParenthesis = 1;
            do
            {
                token = GetNextToken(tokenList);
                if (firstToken == null)
                {
                    firstToken = token;
                }

                if (token.TokenType == EslTokenType.Operator)
                {
                    if (token.TokenValue == "(")
                    {
                        openParenthesis++;
                    }

                    if (token.TokenValue == ")")
                    {
                        openParenthesis--;
                    }
                }
            } 
            while (openParenthesis > 0);

            OCL = ecoScript.GetTextBetweenTokens(firstToken, MyTokens[MyTokens.Count - 1]);

            if (fullExpression)
            {
                EslToken tokenEnd = GetNextToken(tokenList);
                AssertInstructionEnd(tokenEnd);
            }
        }
    }
}